'   Copyright (C) 2011  Jan-Willem de Bruyn

'   This program is free software: you can redistribute it and/or modify
'   it under the terms of the GNU General Public License as published by
'   the Free Software Foundation, either version 3 of the License, or
'   (at your option) any later version.
'
'   This program is distributed in the hope that it will be useful,
'   but WITHOUT ANY WARRANTY; without even the implied warranty of
'    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'    GNU General Public License for more details.

'    You should have received a copy of the GNU General Public License
'    along with this program.  If not, see <http://www.gnu.org/licenses/>. 

 Imports System.IO
Public Class Form1
    Dim streamread As StreamReader
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim a_strArgs() As String
        Dim strFilename As String
        Dim streamread2 As StreamReader = streamread
        Dim i As Integer
        a_strArgs = Split(Command$, " ")
        For i = LBound(a_strArgs) To UBound(a_strArgs)
            Select Case LCase(a_strArgs(i))
                Case "-f", "/f"
                    ' filename specified
                    If i = UBound(a_strArgs) Then
                        streamread2 = New StreamReader("database.psy")
                    Else
                        i = i + 1
                        strFilename = a_strArgs(i)
                        streamread2 = New StreamReader(strFilename)
                    End If
                Case Else
                    streamread2 = New StreamReader("database.psy")
            End Select
        Next i
        For Each arg As String In Environment.GetCommandLineArgs()
            If arg Like "-o*" Then
                streamread2 = New StreamReader(arg.Substring(arg.IndexOf("-o" + 3)))
            End If
        Next
        ListBox1.Items.Clear()
        Dim input As String = streamread2.ReadToEnd
        Dim middle() As String = input.Split(";")
        Dim str1() As String = middle.GetValue(0).ToString.Split(",")
        Dim num As Integer = 0
        For Each Str As String In str1
            num += 1
            If num > 1 Then
                ListBox1.Items.Add(SimpleCrypt(Str))
            End If
        Next
        streamread2.Close()
    End Sub

    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        Dim streamread2 As StreamReader = streamread
        Dim a_strArgs() As String
        Dim strFilename As String
        Dim i As Integer
        a_strArgs = Split(Command$, " ")
        For i = LBound(a_strArgs) To UBound(a_strArgs)
            Select Case LCase(a_strArgs(i)) 
                Case "-f", "/f"
                    ' filename specified
                    If i = UBound(a_strArgs) Then
                        streamread2 = New StreamReader("database.psy")
                    Else
                        i = i + 1
                        strFilename = a_strArgs(i)
                        streamread2 = New StreamReader(strFilename)
                    End If
                Case Else
            End Select
        Next i
        Dim input As String = streamread2.ReadToEnd
        streamread2.Close()
        Dim middle() As String = input.Split(";")
        Dim str1() As String = middle.GetValue(1).ToString.Split(",")
        Dim replys As DialogResult = MessageBox.Show(SimpleCrypt(str1.GetValue(ListBox1.SelectedIndex + 1)) & " do you want download link?", "info", MessageBoxButtons.YesNo)
        Dim reply As DialogResult
        Dim str2() As String = middle.GetValue(2).ToString.Split(",")
        If replys = DialogResult.Yes Then
            reply = MessageBox.Show(SimpleCrypt(str2.GetValue(ListBox1.SelectedIndex + 1)) & " goto link?", "download link", MessageBoxButtons.OKCancel)
        End If
        If reply = DialogResult.Yes Then
            If str2.GetValue(ListBox1.SelectedIndex + 1).ToString.StartsWith("http://") & str2.GetValue(ListBox1.SelectedIndex + 1).ToString.EndsWith("/") Then
                System.Diagnostics.Process.Start(str2.GetValue(ListBox1.SelectedIndex + 1))
            End If
        End If
    End Sub
    Public Function SimpleCrypt( _
       ByVal Text As String) As String
        ' Encrypts/decrypts the passed string using 
        ' a simple ASCII value-swapping algorithm
        Dim strTempChar As String, i As Integer
        For i = 1 To Len(Text)
            If Asc(Mid$(Text, i, 1)) < 128 Then
                strTempChar = _
          CType(Asc(Mid$(Text, i, 1)) + 128, String)
            ElseIf Asc(Mid$(Text, i, 1)) > 128 Then
                strTempChar = _
          CType(Asc(Mid$(Text, i, 1)) - 128, String)
            End If
            Mid$(Text, i, 1) = _
                Chr(CType(strTempChar, Integer))
        Next i
        Return Text
    End Function
End Class
